<?php
namespace app\home\controller;

use app\admin\model\UserModel;
use think\Controller;
use think\Db;
use codes\Qrcode;
/*-------------------------------马鹏程------------------------------------------*/
#用户接口
class Users extends Base
{
    protected $userId;
    public function __construct()
    {
        parent::__construct();
        $this->userId = session('uid');
        if (!$this->userId) {
            return json(['status' => 2000, 'message' => '请先登录', 'data' => '']);
        }

    }
     public function userInfo()
    {
        $model   = new UserModel();
        $user    = $model->field('phone,nickname,pool,activity_pool,type')->where('id', $this->userId)->find();
        return json(['status' => 200, 'message' => '请求成功', 'data' => $user]);
    }

    #编辑用户信息
    public function editUserInfo()
    {
        #实例化模型
        $model = new UserModel();
        $data  = [];
        #获取当前用户信息
        $user = $model->where('id', Session::get('uid'))->find();
        if (input('param.nickname') && input('param.nickname') != '') {
            $data['nickname'] = input('param.nickname');
        }
        if (input('param.truename') && input('param.truename') != '') {
            $data['truename'] = input('param.truename');
        }
        if (input('param.sex') && input('param.sex') != '') {
            $data['sex'] = input('param.sex');
        }
        if (input('param.id_card') && input('param.id_card') != '') {
            $data['id_card'] = input('param.id_card');
        }
        if (input('param.nationality') && input('param.nationality') != '') {
            $data['nationality'] = input('param.nationality');
        }
        if (input('param.province') && input('param.province') != '') {
            $data['province'] = input('param.province');
        }
        if (input('param.city') && input('param.city') != '') {
            $data['city'] = input('param.city');
        }
        if (count($data) < 1) {
            return json(['status' => 300, 'message' => '沒有修改任何數據']);
        }
        $res = $model->where('id', $this->userId)->update($data);
        if ($res) {
            return json(['status' => 200, 'message' => '修改成功']);
        } else {
            return json(['status' => -1, 'message' => '修改失敗']);
        }
    }

     #修改登录密码
    public function getPhone()
    {
        $user = db('users')->where(['id' => $this->userId])->find();

        return json(['status' => 200, 'message' => '查詢成功', 'data' => $user['phone'],'type' => $user['type']]);
    }
    #修改登录密码
    public function editPwd()
    {
        #需要验证码code ,密码password,再次密码two_password

        $user         = db('users')->where(['id' => $this->userId])->find();
        $phone        = $user['phone'];
        $code         = input('code');
        $password     = input('password');
        $two_password = input('qr_password');
        if ($password != $two_password) {
            return json(['status' => -1, 'message' => '密碼不匹配']);
        }
        $prg = "/\w{6,18}$/";
        if (!preg_match($prg, $password)) {
            return json(['status' => -1, 'message' => '密碼長度在6~18之間，只能包含字符、數字或下劃線']);
        }
        if (!$code) {
            return json(['status' => -1, 'message' => '驗證碼不能為空']);
        }
        if(!in_array($phone, config('phones'))){
            if (input('param.code') != db('sms')->where(['phone' => $phone, 'status' => 0, 'type' => 4])->value('code')) {
                return json(['status' => -1, 'message' => '驗證碼錯誤']);
            }
            db('sms')->where(['phone' => $phone, 'status' => 0])->update(['status' => 1, 'type' => 4]);

        }

        $update['password'] = md5($password);
        db('users')->where(['id' => $this->userId])->update($update);
        return json(['status' => 200, 'message' => '修改登錄密碼成功']);
    }
    #修改支付密码
    public function payPwd()
    {
        #需要验证码code ,密码password,再次密码two_password
        $user         = db('users')->where(['id' => $this->userId])->find();
        $phone        = $user['phone'];
        $code         = input('code');
        $password     = input('password');
        $two_password = input('qr_password');
        if ($password != $two_password) {
            return json(['status' => -1, 'message' => '密碼不匹配']);
        }
        $prg = "/\d{6}$/";
        if (!preg_match($prg, $password)) {
            return json(['status' => -1, 'message' => '密碼為6位數字']);
        }
        if (!$code) {
            return json(['status' => -1, 'message' => '驗證碼不能為空']);
        }
        if(!in_array($phone, config('phones'))){
            if (input('param.code') != db('sms')->where(['phone' => $phone, 'status' => 0, 'type' => 5])->value('code')) {
                return json(['status' => -1, 'message' => '驗證碼錯誤']);
            }
            db('sms')->where(['phone' => $phone, 'status' => 0])->update(['status' => 1]);
        }

        $update['two_password'] = md5($password);
        db('users')->where(['id' => $this->userId])->update($update);
        return json(['status' => 200, 'message' => '修改支付密碼成功']);
    }
    #修改手机号n
    public function editPhone()
    {
    	$user   = db('users')->where(['id' => $this->userId])->find();
        $phone  = $user['phone'];
        $param  = input('param.');
        if(!in_array($phone, config('phones'))){
            if(!$param['code'] || !$param['two_code'] || !$param['two_phone']){
                return json(['status' => -1, 'message' => '請完善提交信息']);
            }
            $code1 = db('sms')->where(['phone' => $phone, 'status' => 0, 'type' => 6])->value('code');
            $code2   = db('sms')->where(['phone' => $param['two_phone'], 'status' => 0, 'type' => 6])->value('code');
            if($param['code'] != $code1 || $param['two_code'] != $code2){
                return json(['status' => -1, 'message' => '驗證碼錯誤']);
            }
        }
        

        $update['phone'] = $param['two_phone'];
        if(db('users')->where(['phone' => $param['two_phone']])->find()){
            return json(['status' => -1, 'message' => '該手機號已註冊']);
        }
        db('users')->where(['id' => $this->userId])->update($update);
        return json(['status' => 200, 'message' => '修改手機號成功']);
    }
    #充值
    public function recharge()
    {
    	$model   = new UserModel();
        $user    = $model->field('phone,nickname,pool,activity_pool,type,stack')->where('id', $this->userId)->find();
        if(!is_file('./uploads/qrcode/'.$user['phone'].'.png')){
            $path = ROOT_PATH.'public/uploads/qrcode/'.$user['phone'].'.png';
            Qrcode::png($user['stack'],$path);
        }
        $user['qrcode'] =  DOMAIN.'/uploads/qrcode/'.$user['phone'].'.png';
        return json(['status' => 200, 'message' => '请求成功', 'data' => $user]);
    }



}
